package co.edu.udem.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import co.edu.udem.dao.UserDao;
import co.edu.udem.domain.User;


/**
 * Service for processing Persons
 * 
 */
@Service("userServices")
public class UserServices {

  @Autowired
  private UserDao userDao;

  /**
   * Retrieves all persons
   * 
   * @return a list of persons
   */
  public List<User> getAll() {
    return userDao.getAllUsers();
  }

  /**
   * Retrieves a single person
   */
  public User get(String userCode) {
    return userDao.getUserById(userCode);
  }

  /**
   * Adds a new person
   */
  public void add(User user) {
    userDao.saveUser(user);
  }

  /**
   * Deletes an existing person
   * @param userCode the id of the existing person
   */
  public void delete(String userCode) {
    userDao.deleteUser(userCode);
  }

  /**
   * Edits an existing person
   */
  public void edit(User user) {

    User existingPerson = userDao.getUserById(user.getUserCode());

    // Assign updated values to this person
    existingPerson.setName(user.getName());
    existingPerson.setUserCode(user.getUserCode());
    existingPerson.setPassword(user.getPassword());
    existingPerson.setUserType(user.getUserType());
    existingPerson.setUserTypeDescription(UserTypeEnum.getUserTypeDescriptionByCode(user.getUserType()));

    // Save updates
    userDao.saveUser(existingPerson);
  }
}
